package org.code.core;


import java.io.File; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import org.code.dbconn.ConnectionPond; 
import org.code.dbconn.MyDataSource; 
import org.code.util.AttrTpyeChange; 
import org.code.util.ExceptFile; 
import org.code.util.GetSet; 
import org.code.util.NormName; 
import org.code.util.Prop;


public class CreateCode
{
	 private static String schema = "";
	private static String tables = "";
	private static String db2tables = "";
	private static String descTable = "";
	private static String codePath = "";
	private static String savePath = "";
	private static String rootPath = "";
	private static String propFileName = "createcode.properties";
	private static List<String> tableList = new ArrayList<String>();
	private static Map<String, List<TableBean>> tableDescMap = new HashMap<String, List<TableBean>>();

	
	public CreateCode()
	{
		
		try {
			Prop prop = new Prop();
			Map propMap = prop.getProp(propFileName);
			db2tables=(String) propMap.get("sql.db2tables");
			tables = (String) propMap.get("sql.tables");
			  schema=(String)propMap.get("sql.schema");
			tables=tables.replace("?", schema);
			
			descTable = (String) propMap.get("sql.descTable");
			codePath = (String) propMap.get("code.path");
			savePath = (String) propMap.get("code.savePath");
			if ((savePath == null) || (savePath.isEmpty()))
				savePath = System.getProperty("user.dir").replaceAll(
						"\\\\", "/");
			}
		catch (Exception e)
		{
			e.printStackTrace();
			}
		getDbInfo();
		createPackage();
		//DB2
		CreateMapperDB2();
		//mysql
	//	CreateMapperMysql();
		CreatePojo();
		CreateDao();
		CreateService();
		CreateUtil();
		CreateImpl();
		CreateConfig();
		CreateJdbcProperties();
		}

	
	public void createPackage()
	{
		if (savePath.lastIndexOf("/") != savePath.length() - 1) {
			savePath += "/";
			}
		savePath += "src/";
		rootPath = savePath;
		
		String root = savePath;
		
		String[] pathArr = codePath.split("[.]");
		
		for (int i = 0; i < pathArr.length; i++) {
			savePath = savePath + pathArr[i] + "/";
			}
		File file = null;
		
		String[] packageArr = { root + "resource", savePath + "dao",
				savePath + "pojo", savePath + "service",
				savePath + "service/impl", savePath + "util" };
		
		for (int i = 0; i < packageArr.length; i++) {
			file = new File(packageArr[i]);
			file.mkdirs();
			}
		}

	
	public void getDbInfo()
	{
		ConnectionPond.getInstance();
		MyDataSource mds = ConnectionPond.getMds();
		try
		{
			Connection conn = mds.getConnection();
			
			PreparedStatement ps = conn.prepareStatement(tables);
			
			ResultSet rs = ps.executeQuery();
			
			while (rs.next()) {
				tableList.add(rs.getString(1));
				}
			
			TableBean tableBean = null;
			List<TableBean> tbList = null;
			for (int i = 0; i < tableList.size(); i++)
			{
//				ps = conn.prepareStatement(descTable.replace("?",
//						(CharSequence) tableList.get(i)));
				String sqltabls=descTable.replace("?", tableList.get(i));
				sqltabls=sqltabls.replace("|", schema);
				ps=conn.prepareStatement(sqltabls);
				
				rs = ps.executeQuery();
				
				tbList = new ArrayList<TableBean>();
				
				while (rs.next())
				{
					tableBean = new TableBean();
					tableBean.setField(rs.getString(1));
					tableBean.setType(rs.getString(2));
					tableBean.setIsNull(rs.getString(3));
					tableBean.setKey(rs.getString(5));
					tableBean.setDefaults(rs.getString(4));
					
					tbList.add(tableBean);
					}
				
				tableDescMap.put((String) tableList.get(i), tbList);
				}
			
			rs.close();
			
			ps.close();
			
			mds.free(conn);
			}
		catch (SQLException e)
		{
			e.printStackTrace();
			}
		}

	
	public void CreatePojo()
	{
		for (String tableName : tableList)
		{
			List<TableBean> tableList = tableDescMap.get(tableName);
			
			String fileStr = "";
			
			String table = NormName.camelHump(tableName);
			
			fileStr = "public class " + table + " {\r\n\r\n";
			
			int i = 0;
			
			for (TableBean tableBean : tableList)
			{
				String name = NormName.firLower(NormName
						.camelHump(tableBean.getField()));
				
				String type = AttrTpyeChange.changeType(tableBean
						.getType());
				
				if (("Date".equals(type)) && (i++ == 0)) {
					fileStr = "import java.util.Date;\r\n\r\n"
							+ fileStr;
					}
				
				fileStr = fileStr + "\tprivate " + type + " " + name
						+ ";\r\n";
				fileStr = fileStr + GetSet.set(type, name);
				fileStr = fileStr + GetSet.get(type, name);
				}
			
			fileStr = "package " + codePath + "." + "pojo"
					+ ";\r\n\r\n" + fileStr + "}";
			
			ExceptFile.writeFile(fileStr, savePath + "pojo" + "/"
					+ table + ".java");
			}
		}

	
	public void CreateDao()
	{
		String daoXmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		daoXmlStr = daoXmlStr
				+ "<beans  xmlns=\"http://www.springframework.org/schema/beans\"\r\n";
		daoXmlStr = daoXmlStr
				+ "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n";
		daoXmlStr = daoXmlStr
				+ "\txsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n";
		daoXmlStr = daoXmlStr
				+ "\thttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd\">\r\n\r\n";
		
		for (String tableName : tableList)
		{
			String fileStr = "";
			
			String table = NormName.camelHump(tableName);
			String dao = NormName.firUpper("dao");
			
			fileStr = "import " + codePath + "." + "pojo" + "."
					+ table + ";\r\n\r\n";
			fileStr = fileStr + "import java.util.List;\r\n\r\n";
			fileStr = fileStr + "import java.util.Map;\r\n\r\n";
			fileStr = fileStr + "public interface " + table + dao
					+ " {\r\n\r\n";
			fileStr = fileStr + "\tpublic void insert(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic List<" + table + "> select("
					+ table + " " + table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic void update(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic void delete(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic List<" + table
					+ "> selectByParam(Map<String, String> param);\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void updateByParam(Map<String, String> params);\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void deleteByParam(Map<String, String> params);\r\n\r\n";
			fileStr = "package " + codePath + "." + "dao"
					+ ";\r\n\r\n" + fileStr + "}\r\n\r\n";
			
			ExceptFile.writeFile(fileStr, savePath + "dao" + "/"
					+ table + dao + ".java");
			daoXmlStr = daoXmlStr
					+ "\t<bean id=\""
					+ table
					+ dao
					+ "\" class=\"org.mybatis.spring.mapper.MapperFactoryBean\">\r\n  \t\t<property name=\"sqlSessionFactory\" ref=\"sqlSessionFactory\" />  \r\n \t\t<property name=\"mapperInterface\" value=\""
					+ codePath + "." + "dao" + "." + table + dao
					+ "\" /> \r\n \t</bean>\r\n\r\n";
			}
		
		daoXmlStr = daoXmlStr + "</beans>";
		ExceptFile.writeFile(daoXmlStr, rootPath + "/spring-dao.xml");
		}

	
	public void CreateService()
	{
		String serviceXmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		serviceXmlStr = serviceXmlStr
				+ "<beans  xmlns=\"http://www.springframework.org/schema/beans\"\r\n";
		serviceXmlStr = serviceXmlStr
				+ "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n";
		serviceXmlStr = serviceXmlStr
				+ "\txsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n";
		serviceXmlStr = serviceXmlStr
				+ "\thttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd\">\r\n\r\n";
		
		for (String tableName : tableList)
		{
			String fileStr = "";
			
			String table = NormName.camelHump(tableName);
			String dao = NormName.firUpper("dao");
			String service = NormName.firUpper("service");
			fileStr = "import " + codePath + "." + "pojo" + "."
					+ table + ";\r\n\r\n";
			fileStr = fileStr + "import java.util.List;\r\n\r\n";
			fileStr = fileStr + "public interface " + table + service
					+ " {\r\n\r\n";
			fileStr = fileStr + "\tpublic void insert(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic List<" + table + "> select("
					+ table + " " + table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic void update(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic void delete(" + table + " "
					+ table.toLowerCase() + ");\r\n\r\n";
			fileStr = fileStr + "\tpublic List<" + table
					+ "> selectByParam(String param);\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void updateByParam(String param);\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void deleteByParam(String param);\r\n\r\n";
			fileStr = "package " + codePath + "." + "service"
					+ ";\r\n\r\n" + fileStr + "}\r\n\r\n";
			
			ExceptFile.writeFile(fileStr, savePath + "service" + "/"
					+ table + service + ".java");
			serviceXmlStr = serviceXmlStr + "\t<bean id=\"" + table
					+ service + "\" class=\"" + codePath + "." + "service"
					+ ".impl." + table + "Impl\">\r\n \t\t<property name=\""
					+ NormName.firLower(table) + dao + "\" ref=\"" + table
					+ dao + "\" />\r\n \t</bean>\r\n\r\n";
			}
		
		serviceXmlStr = serviceXmlStr + "</beans>";
		ExceptFile.writeFile(serviceXmlStr, rootPath
				+ "/spring-service.xml");
		}

	
	public void CreateImpl()
	{
		for (String tableName : tableList)
		{
			String fileStr = "";
			
			String table = NormName.camelHump(tableName);
			String tableAsix = table.toLowerCase();
			
			String service = NormName.firUpper("service");
			String dao = NormName.firUpper("dao");
			String type = table + dao;
			String name = NormName.firLower(table) + dao;
			fileStr = fileStr + "import " + codePath + "." + "pojo"
					+ "." + table + ";\r\n";
			fileStr = fileStr + "import " + codePath + "." + "dao"
					+ "." + table + dao + ";\r\n";
			fileStr = fileStr + "import " + codePath + "." + "service"
					+ "." + table + service + ";\r\n";
			fileStr = fileStr + "import " + codePath + "." + "util"
					+ ".NormName;\r\n";
			fileStr = fileStr + "import java.util.HashMap;\r\n";
			fileStr = fileStr + "import java.util.List;\r\n";
			fileStr = fileStr + "import java.util.Map;\r\n\r\n";
			fileStr = fileStr + "public class " + table
					+ "Impl implements " + table + service + " {\r\n\r\n";
			fileStr = fileStr + "\tprivate " + type + " " + name
					+ ";\r\n";
			fileStr = fileStr + GetSet.set(type, name) + "\r\n";
			
			fileStr = fileStr + "\tpublic void insert(" + table + " "
					+ tableAsix + "){\r\n\t\t" + name + ".insert(" + tableAsix
					+ ");\r\n\t}\r\n\r\n";
			fileStr = fileStr + "\tpublic List<" + table + "> select("
					+ table + " " + tableAsix + "){\r\n\t\treturn " + name
					+ ".select(" + tableAsix + ");\r\n\t}\r\n\r\n";
			fileStr = fileStr + "\tpublic void update(" + table + " "
					+ tableAsix + "){\r\n\t\t" + name + ".update(" + tableAsix
					+ ");\r\n\t}\r\n\r\n";
			fileStr = fileStr + "\tpublic void delete(" + table + " "
					+ tableAsix + "){\r\n\t\t" + name + ".delete(" + tableAsix
					+ ");\r\n\t}\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic List<"
					+ table
					+ "> selectByParam(String param){\r\n\t\tMap<String, String> map = new HashMap<String, String>();\r\n\t\tmap.put(\"param\", NormName.normSql(param));\r\n\t\t return "
					+ name + ".selectByParam(map);\r\n\t}\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void updateByParam(String param){\r\n\t\tMap<String, String> map = new HashMap<String, String>();\r\n\t\tmap.put(\"param\", NormName.normSql(param));\r\n\t\t  "
					+ name + ".updateByParam(map);\r\n\t}\r\n\r\n";
			fileStr = fileStr
					+ "\tpublic void deleteByParam(String param){\r\n\t\tMap<String, String> map = new HashMap<String, String>();\r\n\t\tmap.put(\"param\", NormName.normSql(param));\r\n\t\t  "
					+ name + ".deleteByParam(map);\r\n\t}\r\n\r\n";
			fileStr = "package " + codePath + "." + "service"
					+ ".impl;\r\n\r\n" + fileStr + "}\r\n\r\n";
			
			ExceptFile.writeFile(fileStr, savePath + "service/impl"
					+ "/" + table + "Impl" + ".java");
			}
		}

	
	public void CreateMapperDB2()
	{
		String mybatisXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		mybatisXml = mybatisXml
				+ "<!DOCTYPE configuration PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\"\r\n";
		mybatisXml = mybatisXml
				+ "\t\"http://mybatis.org/dtd/mybatis-3-config.dtd\">\r\n\r\n";
		mybatisXml = mybatisXml + "<configuration>\r\n\t<mappers>\r\n";
		
		for (String tableName : tableList)
		{
			List<TableBean> tableList = tableDescMap.get(tableName);
			
			String fileStr = "";
			String dao = NormName.firUpper("dao");
			String table = NormName.camelHump(tableName);
			
			fileStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
			fileStr = fileStr
					+ "<!DOCTYPE mapper  PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\r\n";
			fileStr = fileStr
					+ "\t\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n\r\n";
			fileStr = fileStr + "<mapper namespace=\"" + codePath
					+ "." + "dao" + "." + table + dao + "\">\r\n\r\n";
			
			fileStr = fileStr + "\t<resultMap id=\"" + table
					+ "Map\" type=\"" + codePath + "." + "pojo" + "." + table
					+ "\">\r\n";
			
			String resultMap = "";
			String paramStr = "";
			String resultStr = "";
			String updateStr = "";
			String id = "";
			String columnId = "";
			String contionStr = "";
			
			for (TableBean tableBean : tableList)
			{
				String name = NormName.firLower(NormName
						.camelHump(tableBean.getField()));
				
				if (tableBean.getField().equals(tableBean.getKey())) {
					resultMap = resultMap + "\t\t<id property=\""
							+ name + "\" column=\"" + tableBean.getField()
							+ "\"/>\r\n";
					id = name;
					columnId = tableBean.getField();
					contionStr = contionStr + "\t\t\t<if test=\""
							+ name + " != null\"> and " + tableBean.getField()
							+ " = #{" + name + "}</if>\r\n";
					} else {
					resultMap = resultMap + "\t\t<result property=\""
							+ name + "\" column=\"" + tableBean.getField()+"\""+"\tjdbcType=\""+AttrTpyeChange.changes(tableBean.getType())
							+ "\"/>\r\n";
					
					paramStr = paramStr + tableBean.getField() + ",";
					resultStr = resultStr + "#{" + name + ",jdbcType="+AttrTpyeChange.changes(tableBean.getType())+"},";
					
					updateStr = updateStr + "\t\t\t<if test=\"" + name
							+ "!=null\">" + tableBean.getField() + " = #{"
							+ name + "},</if>\r\n";
					
					contionStr = contionStr + "\t\t\t<if test=\""
							+ name + " != null\"> and " + tableBean.getField()
							+ " = #{" + name + "}</if>\r\n";
					}
				
				}
			
			fileStr = fileStr + resultMap + "\t</resultMap>\r\n\r\n";
		 
			if(!paramStr.isEmpty()){
				paramStr = paramStr.substring(0, paramStr.length() - 1);
			}
			if(!resultStr.isEmpty()){
				resultStr = resultStr.substring(0, resultStr.length() - 1);
			}
			
			fileStr = fileStr
					+ "\t<insert id=\"insert\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tinsert into  " + tableName + " ("
					+ paramStr + ") values(" + resultStr + ")\t\r\n";
			fileStr = fileStr + "\t</insert>\r\n\r\n";
			if(!updateStr.isEmpty()){
			updateStr = updateStr.substring(0, updateStr
					.lastIndexOf(","))
					+ "</if>\r\n";
			}
			fileStr = fileStr
					+ "\t<update id=\"update\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tupdate " + tableName + "\r\n";
			fileStr = fileStr + "\t\t<set>\r\n";
			fileStr = fileStr + updateStr;
			fileStr = fileStr + "\t\t</set>\r\n";
			fileStr = fileStr + "\t\twhere " + columnId + " = #{" + id
					+ "}\r\n";
			fileStr = fileStr + "\t</update>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<delete id=\"delete\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tdelete from " + tableName
					+ " where 1=1\r\n";
			fileStr = fileStr + contionStr + "\t</delete>\r\n\r\n";
			
			fileStr = fileStr + "\t<select id=\"select\" resultMap=\""
					+ table + "Map\" parameterType=\"" + codePath + "."
					+ "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tselect * from " + tableName
					+ " where 1=1\r\n";
			fileStr = fileStr + contionStr + "\t</select>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<select id=\"selectByParam\" resultMap=\"" + table
					+ "Map\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\tselect * from "
					+ tableName
					+ " <if test=\"param != null and param != ''\">where ${param}</if>\r\n";
			fileStr = fileStr + "\t</select>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<delete id=\"deleteByParam\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\tdelete from "
					+ tableName
					+ "  <if test=\"param != null and param != ''\">where ${param}</if>\r\n";
			fileStr = fileStr + "\t</delete>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<update id=\"updateByParam\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\t<if test=\"param != null and param !=''\">update "
					+ tableName + " set ${param}</if>\r\n";
			fileStr = fileStr + "\t</update>\r\n\r\n";
			
			fileStr = fileStr + "</mapper>";
			
			ExceptFile.writeFile(fileStr, rootPath + "resource" + "/"
					+ table + ".xml");
			mybatisXml = mybatisXml
					+ "\t\t<mapper resource=\"resource/" + table
					+ ".xml\"/>\r\n";
			}
		mybatisXml = mybatisXml + "\t</mappers>\r\n</configuration>";
		ExceptFile.writeFile(mybatisXml, rootPath
				+ "mybatis-config.xml");
		}

	
	public void CreateMapperMysql()
	{
		String mybatisXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		mybatisXml = mybatisXml
				+ "<!DOCTYPE configuration PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\"\r\n";
		mybatisXml = mybatisXml
				+ "\t\"http://mybatis.org/dtd/mybatis-3-config.dtd\">\r\n\r\n";
		mybatisXml = mybatisXml + "<configuration>\r\n\t<mappers>\r\n";
		
		for (String tableName : tableList)
		{
			List<TableBean> tableList = tableDescMap.get(tableName);
			
			String fileStr = "";
			String dao = NormName.firUpper("dao");
			String table = NormName.camelHump(tableName);
			
			fileStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
			fileStr = fileStr
					+ "<!DOCTYPE mapper  PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\r\n";
			fileStr = fileStr
					+ "\t\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\r\n\r\n";
			fileStr = fileStr + "<mapper namespace=\"" + codePath
					+ "." + "dao" + "." + table + dao + "\">\r\n\r\n";
			
			fileStr = fileStr + "\t<resultMap id=\"" + table
					+ "Map\" type=\"" + codePath + "." + "pojo" + "." + table
					+ "\">\r\n";
			
			String resultMap = "";
			String paramStr = "";
			String resultStr = "";
			String updateStr = "";
			String id = "";
			String columnId = "";
			String contionStr = "";
			
			for (TableBean tableBean : tableList)
			{
				String name = NormName.firLower(NormName
						.camelHump(tableBean.getField()));
				
				if ("PRI".equals(tableBean.getKey())) {
					resultMap = resultMap + "\t\t<id property=\""
							+ name + "\" column=\"" + tableBean.getField()
							+ "\"/>\r\n";
					id = name;
					columnId = tableBean.getField();
					contionStr = contionStr + "\t\t\t<if test=\""
							+ name + " != null\"> and " + tableBean.getField()
							+ " = #{" + name + "}</if>\r\n";
					} else {
					resultMap = resultMap + "\t\t<result property=\""
							+ name + "\" column=\"" + tableBean.getField()
							+ "\"/>\r\n";
					
					paramStr = paramStr + tableBean.getField() + ",";
					resultStr = resultStr + "#{" + name + "},";
					
					updateStr = updateStr + "\t\t\t<if test=\"" + name
							+ "!=null\">" + tableBean.getField() + " = #{"
							+ name + "},</if>\r\n";
					
					contionStr = contionStr + "\t\t\t<if test=\""
							+ name + " != null\"> and " + tableBean.getField()
							+ " = #{" + name + "}</if>\r\n";
					}
				
				}
			
			fileStr = fileStr + resultMap + "\t</resultMap>\r\n\r\n";
		 
			if(!paramStr.isEmpty()){
				paramStr = paramStr.substring(0, paramStr.length() - 1);
			}
			if(!resultStr.isEmpty()){
				resultStr = resultStr.substring(0, resultStr.length() - 1);
			}
			
			fileStr = fileStr
					+ "\t<insert id=\"insert\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tinsert into  " + tableName + " ("
					+ paramStr + ") values(" + resultStr + ")\t\r\n";
			fileStr = fileStr + "\t</insert>\r\n\r\n";
			if(!updateStr.isEmpty()){
			updateStr = updateStr.substring(0, updateStr
					.lastIndexOf(","))
					+ "</if>\r\n";
			}
			fileStr = fileStr
					+ "\t<update id=\"update\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tupdate " + tableName + "\r\n";
			fileStr = fileStr + "\t\t<set>\r\n";
			fileStr = fileStr + updateStr;
			fileStr = fileStr + "\t\t</set>\r\n";
			fileStr = fileStr + "\t\twhere " + columnId + " = #{" + id
					+ "}\r\n";
			fileStr = fileStr + "\t</update>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<delete id=\"delete\" parameterType=\"" + codePath
					+ "." + "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tdelete from " + tableName
					+ " where 1=1\r\n";
			fileStr = fileStr + contionStr + "\t</delete>\r\n\r\n";
			
			fileStr = fileStr + "\t<select id=\"select\" resultMap=\""
					+ table + "Map\" parameterType=\"" + codePath + "."
					+ "pojo" + "." + table + "\">\r\n";
			fileStr = fileStr + "\t\tselect * from " + tableName
					+ " where 1=1\r\n";
			fileStr = fileStr + contionStr + "\t</select>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<select id=\"selectByParam\" resultMap=\"" + table
					+ "Map\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\tselect * from "
					+ tableName
					+ " <if test=\"param != null and param != ''\">where ${param}</if>\r\n";
			fileStr = fileStr + "\t</select>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<delete id=\"deleteByParam\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\tdelete from "
					+ tableName
					+ "  <if test=\"param != null and param != ''\">where ${param}</if>\r\n";
			fileStr = fileStr + "\t</delete>\r\n\r\n";
			
			fileStr = fileStr
					+ "\t<update id=\"updateByParam\" parameterType=\"java.util.Map\">\r\n";
			fileStr = fileStr
					+ "\t\t<if test=\"param != null and param !=''\">update "
					+ tableName + " set ${param}</if>\r\n";
			fileStr = fileStr + "\t</update>\r\n\r\n";
			
			fileStr = fileStr + "</mapper>";
			
			ExceptFile.writeFile(fileStr, rootPath + "resource" + "/"
					+ table + ".xml");
			mybatisXml = mybatisXml
					+ "\t\t<mapper resource=\"resource/" + table
					+ ".xml\"/>\r\n";
			}
		mybatisXml = mybatisXml + "\t</mappers>\r\n</configuration>";
		ExceptFile.writeFile(mybatisXml, rootPath
				+ "mybatis-config.xml");
		}
	
	
	public void CreateUtil() {
		String utilStr = "package " + codePath + "." + "util"
				+ ";\r\n\r\n";
		utilStr = utilStr + "public class NormName {\r\n\r\n";
		utilStr = utilStr
				+ "\tpublic static String normSql(String sql){\r\n\r\n";
		utilStr = utilStr
				+ "\t\tif(sql == null || sql.isEmpty()){\r\n\r\n";
		utilStr = utilStr + "\t\t\treturn null;\r\n\r\n";
		utilStr = utilStr + "\t\t}\r\n\r\n";
		utilStr = utilStr
				+ "\t\tString sqlTemp = sql.toLowerCase();\r\n\r\n";
		utilStr = utilStr
				+ "\t\tint n = sqlTemp.indexOf(\"set\");\r\n\r\n";
		utilStr = utilStr + "\t\tif(n != -1){\r\n\r\n";
		utilStr = utilStr
				+ "\t\t\treturn sql.substring(n+3, sql.length());\r\n\r\n";
		utilStr = utilStr + "\t\t}\r\n\r\n";
		utilStr = utilStr
				+ "\t\tint i = sqlTemp.indexOf(\"where\");\r\n\r\n";
		utilStr = utilStr + "\t\tif(i != -1){\r\n\r\n";
		utilStr = utilStr
				+ "\t\t\tsql = sql.substring(i+5, sql.length());\r\n\r\n";
		utilStr = utilStr + "\t\t}\r\n\r\n";
		utilStr = utilStr + "\t\treturn sql;\r\n\r\n";
		utilStr = utilStr + "\t}\r\n\r\n";
		utilStr = utilStr + "}\r\n\r\n";
		
		ExceptFile.writeFile(utilStr, savePath + "util"
				+ "/NormName.java");
		}

	
	public void CreateJdbcProperties() {
		Prop prop = new Prop();
		Map propMap = prop.getProp(propFileName);
		String xmlStr = "jdbc.driverClassName="
				+ (String) propMap.get("jdbc.driverClassName") + "\r\n";
		xmlStr = xmlStr + "jdbc.url="
				+ (String) propMap.get("jdbc.url") + "\r\n";
		xmlStr = xmlStr + "jdbc.username="
				+ (String) propMap.get("jdbc.username") + "\r\n";
		xmlStr = xmlStr + "jdbc.password="
				+ (String) propMap.get("jdbc.password") + "\r\n";
		ExceptFile.writeFile(xmlStr, rootPath + "/jdbc.properties");
		}

	
	public void CreateConfig()
	{
		String appStr = "";
		appStr = appStr
				+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		appStr = appStr
				+ "<beans xmlns=\"http://www.springframework.org/schema/beans\"\r\n";
		appStr = appStr
				+ "\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:context=\"http://www.springframework.org/schema/context\"\r\n";
		appStr = appStr
				+ "\txmlns:aop=\"http://www.springframework.org/schema/aop\" xmlns:tx=\"http://www.springframework.org/schema/tx\"\r\n";
		appStr = appStr
				+ "\txsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/context\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/context/spring-context-2.5.xsd\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/aop\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd\r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/tx \r\n";
		appStr = appStr
				+ "           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd\">\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr + " \r\n";
		appStr = appStr + "\r\n";
		appStr = appStr + "\t   <bean id=\"multipartResolver\"\r\n";
		appStr = appStr
				+ "\t\tclass=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\">\r\n";
		appStr = appStr
				+ "\t        <property name=\"maxUploadSize\" value=\"5000000\" />\r\n";
		appStr = appStr
				+ "\t        <property name=\"maxInMemorySize\" value=\"5000000\" />\r\n";
		appStr = appStr
				+ "\t        <property name=\"defaultEncoding\">\r\n";
		appStr = appStr + "\t            <value>UTF-8</value>\r\n";
		appStr = appStr + "\t        </property>\r\n";
		appStr = appStr + "\t    </bean>\r\n";
		appStr = appStr + "\t\r\n";
		appStr = appStr
				+ "\t    <bean name=\"paraMethodResolver\" class=\"org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver\">\r\n";
		appStr = appStr
				+ "\t        <property name=\"paramName\">\r\n";
		appStr = appStr + "\t            <value>method</value>\r\n";
		appStr = appStr + "\t        </property>\r\n";
		appStr = appStr + "\t    </bean>\r\n";
		appStr = appStr + "\t\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr + "  \t <!-- dao -->\r\n";
		appStr = appStr
				+ "\t<import resource=\"classpath:spring-dao.xml\" />\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr + "\t<!-- service -->\r\n";
		appStr = appStr
				+ "\t<import resource=\"classpath:spring-service.xml\" />\r\n";
		appStr = appStr + "\t \r\n";
		appStr = appStr + "\r\n";
		appStr = appStr
				+ "\t<bean class=\"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer\">\r\n";
		appStr = appStr + "\t\t<property name=\"locations\">\r\n";
		appStr = appStr
				+ "\t\t\t<value>classpath:jdbc.properties</value>\r\n";
		appStr = appStr + "\t\t</property>\r\n";
		appStr = appStr + "\t</bean>\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr
				+ "\t<bean id=\"dataSource\" class=\"com.mchange.v2.c3p0.ComboPooledDataSource\"\r\n";
		appStr = appStr + "\t\tdestroy-method=\"close\">\r\n";
		appStr = appStr
				+ "\t\t<property name=\"driverClass\" value=\"${jdbc.driverClassName}\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"jdbcUrl\" value=\"${jdbc.url}\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"user\" value=\"${jdbc.username}\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"password\" value=\"${jdbc.password}\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"minPoolSize\" value=\"2\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"maxPoolSize\" value=\"20\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"maxIdleTime\" value=\"1800\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"acquireIncrement\" value=\"2\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"maxStatements\" value=\"0\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"initialPoolSize\" value=\"3\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"idleConnectionTestPeriod\" value=\"1800\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"acquireRetryAttempts\" value=\"30\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"breakAfterAcquireFailure\" value=\"true\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"testConnectionOnCheckout\" value=\"false\" />\r\n";
		appStr = appStr + "\t</bean>\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr
				+ "\t<bean id=\"sqlSessionFactory\" class=\"org.mybatis.spring.SqlSessionFactoryBean\">\r\n";
		appStr = appStr
				+ "\t\t<property name=\"dataSource\" ref=\"dataSource\" />\r\n";
		appStr = appStr
				+ "\t\t<property name=\"configLocation\" value=\"classpath:mybatis-config.xml\" /> \r\n";
		appStr = appStr + "\t</bean>\r\n";
		appStr = appStr + "\r\n";
		appStr = appStr + " \r\n";
		appStr = appStr + "</beans>\r\n";
		ExceptFile.writeFile(appStr, rootPath
				+ "/applicationContext.xml");
		
		String log4jStr = "";
		log4jStr = log4jStr
				+ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
		log4jStr = log4jStr
				+ "<!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\">\r\n";
		log4jStr = log4jStr + "\r\n";
		log4jStr = log4jStr
				+ "<log4j:configuration xmlns:log4j=\"http://jakarta.apache.org/log4j/\">\r\n";
		log4jStr = log4jStr + "\r\n";
		log4jStr = log4jStr + "    <!-- Console output -->\r\n";
		log4jStr = log4jStr + "\r\n";
		log4jStr = log4jStr
				+ "    <appender name=\"STDOUT\" class=\"org.apache.log4j.ConsoleAppender\">\r\n";
		log4jStr = log4jStr
				+ "        <layout class=\"org.apache.log4j.PatternLayout\">\r\n";
		log4jStr = log4jStr
				+ "            <param name=\"ConversionPattern\" value=\"%-5p %d{MM-dd HH:mm:ss} %m  (%F:%L) \\n\"/>\r\n";
		log4jStr = log4jStr + "        </layout>\r\n";
		log4jStr = log4jStr + "    </appender>\r\n";
		log4jStr = log4jStr + "\r\n";
		log4jStr = log4jStr + "    <category name=\"java.sql\">\r\n";
		log4jStr = log4jStr
				+ "        <priority value=\"debug\" />\r\n";
		log4jStr = log4jStr + "    </category>\r\n";
		log4jStr = log4jStr + "    <category name=\"com.ibatis\">\r\n";
		log4jStr = log4jStr
				+ "        <priority value=\"debug\" />\r\n";
		log4jStr = log4jStr + "    </category>\r\n";
		log4jStr = log4jStr + "    <root>\r\n";
		log4jStr = log4jStr
				+ "        <priority value =\"error\" />\r\n";
		log4jStr = log4jStr
				+ "        <appender-ref ref=\"STDOUT\" />\r\n";
		log4jStr = log4jStr + "    </root>\r\n";
		log4jStr = log4jStr + "\r\n";
		log4jStr = log4jStr + "</log4j:configuration>\r\n";
		ExceptFile.writeFile(log4jStr, rootPath + "/log4j.xml");
		
		String log4jProp = "";
		log4jProp = log4jProp + "#logger level default is INFO\r\n";
		log4jProp = log4jProp
				+ "log4j.rootLogger=info,console,file\r\n";
		log4jProp = log4jProp + "#log4j.rootLogger=info,debug\r\n";
		log4jProp = log4jProp + "log4j.addivity.org.apache=true \r\n";
		log4jProp = log4jProp + "#append to console\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.console=org.apache.log4j.ConsoleAppender\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.console.layout=org.apache.log4j.PatternLayout\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.console.layout.ConversionPattern=%d %p - %m%n\r\n";
		log4jProp = log4jProp + "\r\n";
		log4jProp = log4jProp + "\r\n";
		log4jProp = log4jProp + "log4j.logger.com.neusoft=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.com.opensymphony.oscache=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.net.sf.navigator=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.org.apache.commons=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.org.apache.struts=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.org.displaytag=DEBUG\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.org.springframework=DEBUG\r\n";
		log4jProp = log4jProp + "\r\n";
		log4jProp = log4jProp + "\r\n";
		log4jProp = log4jProp + "#append to file\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file=org.apache.log4j.RollingFileAppender\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file.File=/demo.log\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file.MaxFileSize=1MB\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file.MaxBackupIndex=10000\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file.layout=org.apache.log4j.PatternLayout\r\n";
		log4jProp = log4jProp
				+ "log4j.appender.file.layout.ConversionPattern=%d %p - %m%n\r\n";
		log4jProp = log4jProp + "S\r\n";
		log4jProp = log4jProp + "\r\n";
		log4jProp = log4jProp + "#ibatis logger config\r\n";
		log4jProp = log4jProp + "log4j.logger.com.ibatis=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.com.ibatis.common.jdbc.ScriptRunner=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.java.sql.Connection=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.java.sql.Statement=ERROR\r\n";
		log4jProp = log4jProp
				+ "log4j.logger.java.sql.PreparedStatement=ERROR\r\n";
		ExceptFile
				.writeFile(log4jProp, rootPath + "/log4j.properties");
		}
	  public static void main(String[] args) {
		 new CreateCode(); 
	}
}
